package com.das.mechanic_base.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.alone.X3RecommendForYouAdapter;
import com.das.mechanic_base.bean.alone.TestRecommendBean;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class X3RecommendForYouPopWindow implements View.OnClickListener, X3RecommendForYouAdapter.IonItemClick {

    private Activity mContext;
    private PopupWindow popupWindow;
    private View view;
    private RelativeLayout rl_top;

    public X3RecommendForYouPopWindow(Activity mContext, List<TestRecommendBean> list) {
        this.mContext = mContext;
        view = LayoutInflater.from(mContext).inflate(R.layout.x3_recommend_for_you_popwindow, new LinearLayout(mContext), false);
        RecyclerView recyclerview = view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        X3RecommendForYouAdapter recommendForYouAdapter = new X3RecommendForYouAdapter(mContext);
        recyclerview.setAdapter(recommendForYouAdapter);
        recommendForYouAdapter.setData(list);
        recommendForYouAdapter.setOnItemClick(this);
        initClick();
    }

    private void initClick() {
        TextView tv_all = view.findViewById(R.id.tv_all);
        tv_all.setOnClickListener(this);
        rl_top = view.findViewById(R.id.rl_top);
        rl_top.setOnClickListener(this);
    }

    public void showPopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        MobclickAgent.onPageStart("检测系统猜你推荐弹窗");
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(null);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(mContext.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
//        popupWindow.showAsDropDown(v,0, ScreenUtils.dip2px(20,mContext),Gravity.BOTTOM);

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_all) {
            if (iOnRfyClick != null) {
                iOnRfyClick.iOnClickAll();
            }
        } else if (id == R.id.rl_top) {
            dismissPop();
        }
    }

    @Override
    public void onItemClick(TestRecommendBean testRecommendBean) {
        if (iOnRfyClick != null) {
            iOnRfyClick.iOnItemClick(testRecommendBean);
        }
    }

    public void dismissPop() {
        MobclickAgent.onPageEnd("检测系统猜你推荐弹窗");
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    public interface IOnRfyClick {
        void iOnClickAll();

        void iOnItemClick(TestRecommendBean testRecommendBean);

    }

    IOnRfyClick iOnRfyClick;

    public void setOnRfyClick(IOnRfyClick iOnRfyClick) {
        this.iOnRfyClick = iOnRfyClick;
    }

}
